From d25639716c5c45af1ff8c4f7b7471d12b9b8ddcf Mon Sep 17 00:00:00 2001 From: robertlipe Date: Sun, 5 Oct 2014 23:16:08 +0000 Subject: [PATCH] unicsv: Avoid a few time_t conversions that would lose sub-second. Various: Less CSTR() abuse. garmin_tables. Use QString as arg type. --- gpsbabel/csv_util.cc | 3 +- gpsbabel/garmin_tables.cc | 36 ++++++++++++------------ gpsbabel/garmin_tables.h | 8 +++--- gpsbabel/gpx.cc | 4 +-- gpsbabel/ozi.cc | 4 +-- gpsbabel/text.cc | 15 ++++++---- gpsbabel/unicsv.cc | 58 ++++++++++++++------------------------- gpsbabel/util.cc | 2 +- gpsbabel/xol.cc | 4 +-- 9 files changed, 62 insertions(+), 72 deletions(-) diff --git a/gpsbabel/csv_util.cc b/gpsbabel/csv_util.cc index 8e979fce4..4bf9d01ce 100644 --- a/gpsbabel/csv_util.cc +++ b/gpsbabel/csv_util.cc @@ -881,10 +881,9 @@ addhms(const char* s, const char* format) int hour =0; int min =0; int sec =0; - char* ampm = NULL; int ac; - ampm = (char*) xmalloc(strlen(s) + 1); + char* ampm = (char*) xmalloc(strlen(s) + 1); ac = sscanf(s, format, &hour, &min, &sec, ampm); /* If no time format in arg string, assume AM */ if (ac < 4) { diff --git a/gpsbabel/garmin_tables.cc b/gpsbabel/garmin_tables.cc index cd48db6cc..40412879d 100644 --- a/gpsbabel/garmin_tables.cc +++ b/gpsbabel/garmin_tables.cc @@ -21,6 +21,7 @@ */ #include "garmin_tables.h" +#include "src/core/logging.h" #include "jeeps/gpsmath.h" #include @@ -965,19 +966,19 @@ gt_get_icao_cc(const QString& country, const QString& shortname) } grid_type -gt_lookup_grid_type(const char* grid_name, const char* module) +gt_lookup_grid_type(const char* grid_name, const QString& module) { grid_mapping_t* g; for (g = gt_mps_grid_names; (g->shortname); g++) { - if ((case_ignore_strcmp(grid_name, g->shortname) == 0) || - (case_ignore_strcmp(grid_name, g->longname) == 0)) { + if (QString::compare(grid_name, g->shortname, Qt::CaseInsensitive) == 0 || + QString::compare(grid_name, g->longname,Qt::CaseInsensitive) == 0) { return g->grid; } } - fatal("%s: Unsupported grid (%s)! See GPSBabel help for supported grids.\n", - module, grid_name); + Fatal() << module << ": Unsupported grid (" << grid_name << + ". See GPSBabel help for supported grids.\n"; return grid_unknown; /* (warnings) */ } @@ -1001,7 +1002,7 @@ gt_get_mps_datum_name(const int datum_index) result = GPS_Math_Get_Datum_Name(datum_index); for (d = gt_mps_datum_names; (d->jeeps_name); d++) - if (case_ignore_strcmp(result, d->jeeps_name) == 0) { + if (QString::compare(result, d->jeeps_name, Qt::CaseInsensitive) == 0) { return d->mps_name; } @@ -1009,14 +1010,14 @@ gt_get_mps_datum_name(const int datum_index) } int -gt_lookup_datum_index(const char* datum_str, const char* module) +gt_lookup_datum_index(const char* datum_str, const QString& module) { datum_mapping_t* d; int result; const char* name = datum_str; for (d = gt_mps_datum_names; (d->jeeps_name); d++) { - if (case_ignore_strcmp(name, d->mps_name) == 0) { + if (QString::compare(name, d->mps_name, Qt::CaseInsensitive) == 0) { name = d->jeeps_name; break; } @@ -1024,15 +1025,16 @@ gt_lookup_datum_index(const char* datum_str, const char* module) result = GPS_Lookup_Datum_Index(name); + // Didn't get a hit? Try again after modifying the lookup. if (result < 0) { QString tmp = QString(datum_str) + " mean"; - result = GPS_Lookup_Datum_Index(CSTR(tmp)); + result = GPS_Lookup_Datum_Index(tmp); } - is_fatal(result < 0, - "%s: Unsupported datum (%s)! See GPSBabel help for supported datums.", - module, datum_str); - + if (result < 0) { + Fatal() << module << ": Unsupported datum (" << datum_str << + "). See GPSBabel help for supported datums."; + } return result; } @@ -1047,12 +1049,12 @@ gt_color_value(const unsigned int garmin_index) } uint32_t -gt_color_value_by_name(const char* name) +gt_color_value_by_name(const QString& name) { unsigned int i; for (i = 0; i < GT_COLORS_CT; i++) - if (case_ignore_strcmp(gt_colors[i].name, name) == 0) { + if (QString::compare(gt_colors[i].name, name, Qt::CaseInsensitive) == 0) { return gt_colors[i].rgb; } @@ -1060,12 +1062,12 @@ gt_color_value_by_name(const char* name) } int -gt_color_index_by_name(const char* name) +gt_color_index_by_name(const QString& name) { unsigned int i; for (i = 0; i < GT_COLORS_CT; i++) - if (case_ignore_strcmp(name, gt_colors[i].name) == 0) { + if (QString::compare(gt_colors[i].name, name, Qt::CaseInsensitive) == 0) { return i; } diff --git a/gpsbabel/garmin_tables.h b/gpsbabel/garmin_tables.h index b3069495c..2e2252909 100644 --- a/gpsbabel/garmin_tables.h +++ b/gpsbabel/garmin_tables.h @@ -91,13 +91,13 @@ unsigned char gt_convert_category(const char* name, int* category); unsigned char gt_switch_display_mode_value(const unsigned char display_mode, const int protoid, const char device); -grid_type gt_lookup_grid_type(const char* grid_name, const char* module); +grid_type gt_lookup_grid_type(const char* grid_name, const QString& module); const char* gt_get_mps_grid_longname(const grid_type grid, const char* module); -int gt_lookup_datum_index(const char* datum_str, const char* module); +int gt_lookup_datum_index(const char* datum_str, const QString& module); const char* gt_get_mps_datum_name(const int datum_index); uint32_t gt_color_value(const unsigned int garmin_index); -uint32_t gt_color_value_by_name(const char* name); -int gt_color_index_by_name(const char* name); +uint32_t gt_color_value_by_name(const QString& name); +int gt_color_index_by_name(const QString& name); int gt_color_index_by_rgb(const int rgb); const char* gt_color_name(const unsigned int garmin_index); diff --git a/gpsbabel/gpx.cc b/gpsbabel/gpx.cc index 38e3b1585..7e9337aa4 100644 --- a/gpsbabel/gpx.cc +++ b/gpsbabel/gpx.cc @@ -1033,7 +1033,7 @@ gpx_end(const QString& el) rte_head->rte_desc = cdatastr; break; case tt_garmin_rte_display_color: - rte_head->line_color.bbggrr = gt_color_value_by_name(CSTR(cdatastr)); + rte_head->line_color.bbggrr = gt_color_value_by_name(cdatastr); break; case tt_rte_number: rte_head->rte_num = cdatastr.toInt(); @@ -1064,7 +1064,7 @@ gpx_end(const QString& el) trk_head->rte_desc = cdatastr; break; case tt_garmin_trk_display_color: - trk_head->line_color.bbggrr = gt_color_value_by_name(CSTR(cdatastr)); + trk_head->line_color.bbggrr = gt_color_value_by_name(cdatastr); break; case tt_trk_number: trk_head->rte_num = cdatastr.toInt(); diff --git a/gpsbabel/ozi.cc b/gpsbabel/ozi.cc index 444d5d324..015b3865a 100644 --- a/gpsbabel/ozi.cc +++ b/gpsbabel/ozi.cc @@ -501,7 +501,7 @@ ozi_parse_waypt(int field, const QString& str, Waypoint* wpt_tmp, ozi_fsdata* fs break; case 1: /* waypoint name */ - wpt_tmp->shortname = csv_stringtrim(str, ""); + wpt_tmp->shortname = str.trimmed(); break; case 2: /* degrees latitude */ @@ -541,7 +541,7 @@ ozi_parse_waypt(int field, const QString& str, Waypoint* wpt_tmp, ozi_fsdata* fs break; case 10: /* Description */ - wpt_tmp->description = csv_stringtrim(str, ""); + wpt_tmp->description = str.trimmed(); break; case 11: /* pointer direction 0,1,2,3 bottom,top,left,right */ diff --git a/gpsbabel/text.cc b/gpsbabel/text.cc index f908fa599..bb50d0146 100644 --- a/gpsbabel/text.cc +++ b/gpsbabel/text.cc @@ -130,9 +130,10 @@ text_disp(const Waypoint* wpt) } if (wpt->description != wpt->shortname) { - gbfprintf(file_out, "%s", CSTRc(wpt->description)); + gbfputs(wpt->description, file_out); if (!wpt->gc_data->placer.isEmpty()) { - gbfprintf(file_out, " by %s", CSTR(wpt->gc_data->placer)); + gbfputs(" by ", file_out); + gbfputs(wpt->gc_data->placer, file_out); } } if (wpt->gc_data->terr) { @@ -161,7 +162,9 @@ text_disp(const Waypoint* wpt) xfree(hint); } } else if (!wpt->notes.isEmpty() && (wpt->description.isEmpty() || wpt->notes != wpt->description)) { - gbfprintf(file_out, "\n%s\n", CSTRc(wpt->notes)); + gbfputs("\n", file_out); + gbfputs(wpt->notes, file_out); + gbfputs("\n", file_out); } fs_gpx = NULL; @@ -181,12 +184,14 @@ text_disp(const Waypoint* wpt) logpart = xml_findfirst(curlog, "groundspeak:type"); if (logpart) { - gbfprintf(file_out, "%s by ", CSTR(logpart->cdata)); + gbfputs(logpart->cdata, file_out); + gbfputs(" by ", file_out); } logpart = xml_findfirst(curlog, "groundspeak:finder"); if (logpart) { - gbfprintf(file_out, "%s on ", CSTR(logpart->cdata)); + gbfputs(logpart->cdata, file_out); + gbfputs(" on ", file_out); } logpart = xml_findfirst(curlog, "groundspeak:date"); diff --git a/gpsbabel/unicsv.cc b/gpsbabel/unicsv.cc index fa0196168..c853acf50 100644 --- a/gpsbabel/unicsv.cc +++ b/gpsbabel/unicsv.cc @@ -318,7 +318,8 @@ unicsv_parse_gc_id(const char* str) } else { base = (*str < 'G') ? 16 : 31; } - + // FIXME: this is wrong. 0123456789ABCDEFGHJKMNPQRTVWXYZ ILOSU are omitted. + // That nobody has noticed is a good hint nobody cares... while ((cx = *str++)) { int num; @@ -339,8 +340,6 @@ unicsv_parse_gc_id(const char* str) return res; } -// static int unicsv_parse_time(const char *str, int *msec, time_t *date); - static time_t unicsv_parse_date(const char* str, int* consumed) { @@ -416,7 +415,6 @@ unicsv_parse_time(const char* str, int* msec, time_t* date) *date = ldate; } } - ct = sscanf(str, "%d%1[.://]%d%1[.://]%d%lf", &hour, sep, &min, sep, &sec, &ms); is_fatal(ct < 5, MYNAME ": Could not parse time string (%s).\n", str); if (ct == 6) { @@ -1670,52 +1668,38 @@ unicsv_waypt_disp_cb(const Waypoint* wpt) } if FIELD_USED(fld_date) { if (wpt->creation_time.toTime_t() >= SECONDS_PER_DAY) { - struct tm tm; - char buf[32]; - time_t time = wpt->GetCreationTime().toTime_t(); - + QDateTime dt; if (opt_utc) { - time += atoi(opt_utc) * SECONDS_PER_HOUR; - tm = *gmtime(&time); + dt = wpt->GetCreationTime().toUTC(); + // We might wrap to a different day by overriding the TZ offset. + dt = dt.addSecs(atoi(opt_utc) * SECONDS_PER_HOUR); } else { - const time_t tt = wpt->GetCreationTime().toTime_t(); - tm = *localtime(&tt); + dt = wpt->GetCreationTime(); } - tm.tm_year += 1900; - tm.tm_mon += 1; - snprintf(buf, sizeof(buf), "%04d/%02d/%02d", tm.tm_year, tm.tm_mon, tm.tm_mday); - gbfprintf(fout, "%s%s", unicsv_fieldsep, buf); + QString date = dt.toString("yyyy/MM/dd"); + gbfputs(unicsv_fieldsep, fout); + gbfputs(date, fout); } else { gbfputs(unicsv_fieldsep, fout); } } if FIELD_USED(fld_time) { if (wpt->creation_time.isValid()) { - struct tm tm; - char buf[32], msec[12]; - time_t time = wpt->GetCreationTime().toTime_t(); - + QTime t; if (opt_utc) { - time += atoi(opt_utc) * SECONDS_PER_HOUR; - tm = *gmtime(&time); + t = wpt->GetCreationTime().toUTC().time(); + t = t.addSecs(atoi(opt_utc) * SECONDS_PER_HOUR); } else { - const time_t tt = wpt->GetCreationTime().toTime_t(); - tm = *localtime(&tt); + t = wpt->GetCreationTime().time(); } - snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec); - - int millisecs = wpt->GetCreationTime().time().msec(); - if (millisecs > 0) { - int len = 3; - - while (len && (millisecs % 10 == 0)) { - millisecs /= 10; - len--; - } - snprintf(msec, sizeof(msec), ".%0*d", len, millisecs); - strcat(buf, msec); + QString out; + if (t.msec() > 0) { + out = t.toString("hh:mm:ss.z"); + } else { + out = t.toString("hh:mm:ss"); } - gbfprintf(fout, "%s%s", unicsv_fieldsep, buf); + gbfputs(unicsv_fieldsep, fout); + gbfputs(out, fout); } else { gbfputs(unicsv_fieldsep, fout); } diff --git a/gpsbabel/util.cc b/gpsbabel/util.cc index 076f0bc87..eb8ec5115 100644 --- a/gpsbabel/util.cc +++ b/gpsbabel/util.cc @@ -1,7 +1,7 @@ /* Misc utilities. - Copyright (C) 2002-2005 Robert Lipe, robertlipe+source@gpsbabel.org + Copyright (C) 2002-2014 Robert Lipe, robertlipe+source@gpsbabel.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/gpsbabel/xol.cc b/gpsbabel/xol.cc index 719f0946c..3829bc2a6 100644 --- a/gpsbabel/xol.cc +++ b/gpsbabel/xol.cc @@ -185,8 +185,8 @@ static void xol_write_string(const QString& name, const QString& str) { if (!str.isEmpty()) { - QString temp = strenquote(str, '"'); - gbfprintf(fout, " %s=%s", CSTR(name), CSTR(temp)); + QString out = name + "=" + strenquote(str, '"'); + gbfputs(out, fout); } } -- 2.30.2